/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers
{
	/// <summary>
	/// Provides access to a Groove Tool in a workspace.
	/// </summary>
	public class Tool : GrooveWebServicesV12HelpersBase
	{
		protected GrooveWebServicesV12Helpers.Space m_Space;

		protected GrooveToolsWebService.Tool m_Tool;
		private GrooveToolsWebService.GrooveTools m_GrooveTools;

		public Tool(GrooveWebServicesV12Helpers.Space i_Space, GrooveToolsWebService.Tool i_Tool)
			: base(i_Tool.Name, i_Tool.URI, i_Space.IdentityURL, i_Space.Context)
		{
			m_Space = i_Space;
			m_Tool = i_Tool;
			m_IdentityURL = i_Space.IdentityURL;
		}

		public Tool(string i_IdentityURL, GrooveToolsWebService.Tool i_Tool, GrooveWebServicesV12Helpers.Context i_Context)
			: base(i_Tool.Name, i_Tool.URI, i_IdentityURL, i_Context)
		{
			m_Tool = i_Tool;
			m_IdentityURL = i_IdentityURL;
		}

		#region Tool Properties And Methods

		public static GrooveToolsWebService.GrooveTools CreateInitializedToolsWebService(string i_IdentityURL, string i_TargetURI, GrooveWebServicesV12Helpers.Context i_Context)
		{
			//  Create the service
			GrooveToolsWebService.GrooveTools ToolsWebService = new GrooveToolsWebService.GrooveTools();

			//  Create the Groove SOAP header
			ToolsWebService.GrooveRequestHeaderValue = new GrooveToolsWebService.GrooveRequestHeader();

			//  Set appropriate header values
			ToolsWebService.GrooveRequestHeaderValue.GrooveRequestKey = i_Context.RequestKey;
			ToolsWebService.GrooveRequestHeaderValue.GrooveIdentityURL = i_IdentityURL;

			//  Set the URL
			ToolsWebService.Url = i_Context.Host + i_TargetURI;

			//  Set Timeout
			ToolsWebService.Timeout = i_Context.Timeout;

			return ToolsWebService;
		}

		/// <summary>
		/// The display name of the tool.
		/// </summary>
		public override string Name
		{
			set
			{
				//  Do nothing if value already assigned
				if (m_Tool.Name == value)
					return;

				string OldValue = m_Tool.Name;

				try
				{
					//  Update local instance
					m_Tool.Name = value;

					//  Update data
					int_Update();
				}
				catch (System.Exception Exception)
				{
					//  Revert local instance
					m_Tool.Name = OldValue;

					throw Exception;
				}
			}
		}

		/// <summary>
		/// </summary>
		public string LocalURI
		{
			get
			{
				return m_Tool.LocalURI;
			}
		}

		public string Category
		{
			get
			{
				return m_Tool.Category;
			}
		}

		public string Creator
		{
			get
			{
				return m_Tool.Creator;
			}
		}

		public string Data
		{
			get
			{
				return m_Tool.Data;
			}
		}

		public string ID
		{
			get
			{
				return m_Tool.ID;
			}
		}

		public string Owner
		{
			get
			{
				return m_Tool.Owner;
			}
		}

		public string Type
		{
			get
			{
				return m_Tool.Type;
			}
		}

		public string ComponentResourceURL
		{
			get
			{
				return m_Tool.ComponentResourceURL;
			}
		}

		/// <summary>
		/// The Space in which the Tool is contained.
		/// </summary>
		public GrooveWebServicesV12Helpers.Space Space
		{
			get
			{
				//  If initialized without space, we have to get space uri then call openspace
				if (m_Space == null)
				{
					//  If ToolURI='/GWS/Groove/2.0/Tools/grooveTelespace/gcqz5yr2x36xpjwhdmvvj9wbku9xrz9qxxur9e2/ToolContainer/gvigmdyp9663q'
					//  then SpaceURI='/GWS/Groove/2.0/Spaces/grooveTelespace/gcqz5yr2x36xpjwhdmvvj9wbku9xrz9qxxur9e2'
					string SpaceURI = this.URI;
					SpaceURI = SpaceURI.Substring(0, SpaceURI.IndexOf("/ToolContainer"));
					SpaceURI = SpaceURI.Replace("Tools", "Spaces");

				}

				return m_Space;
			}
		}

		public string MembersURI
		{
			get
			{
				return m_Tool.MembersURI;
			}
		}

		/// <summary>
		/// true if there is unread data in the tool.
		/// </summary>
		public bool IsUnread
		{
			get
			{
				return (m_Tool.Unread > 0);
			}
		}

		/// <summary>
		/// Marks all data in the tool as read.
		/// </summary>
		public void MarkRead()
		{
			//  Get initialized reference to web service
			GrooveToolsWebService.GrooveTools ToolsWebService = this.GetInitializedToolsWebService(this.URI);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to mark tool read
			ToolsWebService.UpdateUnreadMark(false);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("ToolsWebService::UpdateUnreadMark, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		/// <summary>
		/// Marks all data in the tool as unread.
		/// </summary>
		public void MarkUnread()
		{
			//  Get initialized reference to web service
			GrooveToolsWebService.GrooveTools ToolsWebService = this.GetInitializedToolsWebService(this.URI);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to mark tool unread
			ToolsWebService.UpdateUnreadMark(true);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("ToolsWebService::UpdateUnreadMark, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		private void int_Update()
		{
			//  Get initialized reference to web service
			GrooveToolsWebService.GrooveTools ToolsWebService = this.GetInitializedToolsWebService(this.URI);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to update Tool entry
			ToolsWebService.Update(m_Tool);

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("ToolsWebService::Update, " + TimeSpan.TotalMilliseconds + " ms");
			}
		}

		private GrooveToolsWebService.GrooveTools GetInitializedToolsWebService(string i_TargetURI)
		{
			if ((m_GrooveTools == null) || Context.ReinitNeeded(m_GrooveTools.GrooveRequestHeaderValue.GrooveRequestKey))
			{
				m_GrooveTools = GrooveWebServicesV12Helpers.Tool.CreateInitializedToolsWebService
					(
					m_IdentityURL,
					i_TargetURI,
					Context
					);
			}
			else
			{
				//  Explicitly overwrite target URL and Timeout
				m_GrooveTools.Url = Context.Host + i_TargetURI;
                m_GrooveTools.Timeout = Context.Timeout;
			}

			return m_GrooveTools;
		}

		#endregion

		#region GrooveWebServicesV12HelpersBase Implementation

		public override string BindableURL
		{
			get
			{

				if (System.String.IsNullOrEmpty(m_BindableURL))
				{
					//  Sample Tool URI is '/GWS/Groove/2.0/Tools/grooveTelespace/t9r5q57hwpmb4gwy5aju7vg4czgt3jepsvtqqsi/ToolContainer/4rzf7qjcuj2fm4zauwt2qbbwtm9cyczxqzdxe4i.Discussion.Tool',
					//  so we strip off '/GWS/Groove/2.0/Tools/grooveTelespace/t9r5q57hwpmb4gwy5aju7vg4czgt3jepsvtqqsi/ToolContainer/'
					string ToolInfo = this.URI;
					int Index = ToolInfo.LastIndexOf("/") + 1;
					ToolInfo = ToolInfo.Substring(Index);

					m_BindableURL = m_Space.BindableURL + "/Tool#" + ToolInfo;			
				}

				return m_BindableURL;
			}
		}

		#endregion

	}
}
